package com.sm.parameterized;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.springframework.core.io.ClassPathResource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestContextManager;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.util.CollectionUtils;

@RunWith(Parameterized.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
public class BaseTestCase extends AbstractJUnit4SpringContextTests {
	/** 用例ID */
	protected String caseId;
	/** 描述 */
	protected String description;
	/** 期望值 */
	protected String expectValue;
	/** 测试上下文管理 */
	private final TestContextManager testContextManager;

	/** 上面的管理上上下文必须写无参数构造函数初始化 */
	public BaseTestCase(String caseId, String description, String expectValue) {
		this.testContextManager = new TestContextManager(getClass());
		this.caseId = caseId;
		this.description = description;
		this.expectValue = expectValue;
	}

	/**
	 * 依靠<code>DependencyInjectionTestExecutionListener<code>完成注入上下文信息
	 * 
	 * @throws Throwable
	 */
	@Before	
	public void injectDependencies() throws Throwable {
		this.testContextManager.prepareTestInstance(this);
	}

	/**
	 * 获取用例数据
	 * 
	 * @param caseFilePath
	 * @return
	 */
	protected static Collection<String[]> loadCaseData(String caseFilePath) {
		if (StringUtils.isBlank(caseFilePath)) {
			throw new IllegalArgumentException("未初始化用例文件信息:路径为空");
		}
//		List<String[]> caseDataList = FileParser.loadItems("cases/" + caseFilePath);
		List<String[]> caseDataList = FileParser.loadItems(caseFilePath);
		if (CollectionUtils.isEmpty(caseDataList)) {
			throw new IllegalArgumentException("准备数据不能为空:内容为空");
		}
		// 剔除第一行标题信息
		caseDataList.remove(0);
		return caseDataList;
	}

}

/** 内部类实现读取文件规则 **/
class FileParser {
	/** 分隔符 */
	private static final String SPLIT_TAG = ",";
	/** 忽略标记 */
	private static final String IGNORE_TAG = "#";

	/**
	 * 获取文件内容
	 * 
	 * @param filePath
	 * @return
	 */
	public static List<String[]> loadItems(String filePath) {
		List<String[]> itemList = new ArrayList<String[]>();
		ClassPathResource resource = new ClassPathResource(".");
		BufferedReader reader = null;
		try {
			String path = resource.getFile().getAbsolutePath();
			File file = new File(path + "/" + filePath);
			reader = new BufferedReader(new FileReader(file));

			String tempString = null;
			// 一次读入一行，直到读入空为文件结束
			while (StringUtils.isNotBlank(tempString = reader.readLine())) {
				if (tempString.indexOf(IGNORE_TAG) == 0) {
					continue;
				}
				itemList.add(tempString.split(SPLIT_TAG));
			}
			reader.close();

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}
		return itemList;
	}
}
